package com.aaa.ssm.service.impl;

import com.aaa.ssm.dao.StartegyDao;
import com.aaa.ssm.entity.Dicgchannel;
import com.aaa.ssm.entity.Startegy;
import com.aaa.ssm.entity.User;
import com.aaa.ssm.service.StartegyService;
import com.aaa.ssm.util.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class StartegyServiceImpl implements StartegyService {
    @Resource
    private StartegyDao startegyDao;
    @Resource
    HttpSession session;
    @Override
    public PageInfo queryPage(Page<Startegy> page) {
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(startegyDao.queryByParam(page.getData()));
    }
    private static String time(){
        //获取当前时间
        Date date = new Date();
        //实例化格式化时间类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //format格式化时间
        String format = simpleDateFormat.format(date);
        return format;
    }

    @Override
    public PageInfo queryById(Page<Startegy> page) {
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(startegyDao.queryById(page.getData()));
    }

    @Override
    public PageInfo queryByIdA(Page<Dicgchannel> page) {
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        return new PageInfo(startegyDao.queryByIdA(page.getData()));
    }

    @Override
    public int add(Startegy startegy) {
        User userInfo = (User)session.getAttribute("userInfo");
        startegy.setCjtime(time());
        startegy.setCjrid(userInfo.getLoginName());
        startegy.setUpdatetime(time());
        startegy.setUpdaterid(userInfo.getLoginName());
        return startegyDao.add(startegy);
    }

    @Override
    public int update(Startegy startegy) {
        try{
            User userInfo = (User)session.getAttribute("userInfo");
            startegy.setUpdatetime(time());
            startegy.setUpdaterid(userInfo.getLoginName());
            return startegyDao.update(startegy);
        }catch (Exception e){
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(int id) {
        return startegyDao.delete(id);
    }
}
